import { create } from "../state-signals"

interface IUserInfo {
    name: string
    age: number
    sex: string
    firend: {
        name: string
        age: number
    }
    setName: (name: string) => void
}

export const userInfo = create<IUserInfo>(() => {
    return {
        name: '张三',
        age: 18,
        sex: '男',
        firend: {
            name: '李四',
            age: 20,
        },
        setName(name: string) {
            this.name = name
        }
    }
}, "userInfo")

interface AccountState {
    money: number
    balance: number
    coin: number
    addMoney: (money: number) => void
}

export const account = create<AccountState>((set, get) => {
    setTimeout(() => {
        console.log('set account. prev data', get())

        set(prev => {
            prev.money = 100
            prev.balance = 110
            prev.coin = 90
        })
    }, 1e3);

    set({
        money: 999
    })

    return {
        money: 0,
        balance: 100,
        coin: 8,
        addMoney(money: number) {
            this.money += money
        }
    }
}, 'account')
